﻿using ConfExpert.Utility.Grid;
using DevExpress.XtraEditors;
using Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace ConfOrder.Reservation
{
    public partial class Reservations : XForm
    {
        public Reservations()
        {
            InitializeComponent();
        }

        private void Reservations_Load(object sender, EventArgs e)
        {
            GridMenu gmRes = GridManager.GetManager(gvRes).GridMenu;
            gmRes.AddMenu("Release selected reservations", RelaseRes, Properties.Resources.miDelete);

            gvRes.Init("COGetReservations", 75);
            gvRes.AddDetailGrid(gvArtikel, "AyrimId");
            gvRes.AddSummary("TotalQty");
            gvRes.MakeColumnInvisible("AyrimId", "ESPRollCount");
            gvRes.MakeColumnInvisible("RollIds");
            gvRes.FormatAsDate("Expiry");
            gvRes.FormatAsNumber(1, "TotalQty");
            gvRes.MakeReadOnly();
            gvRes.BestFitColumns();

            gvArtikel.MakeColumnInvisible("AyrimId");
            gvArtikel.MakeReadOnly();
            gvArtikel.AddSummary("Qty");
            gvArtikel.FormatAsNumber(1, "Qty");
            gvArtikel.BestFitColumns();

            splitter.SplitterPosition = 700;
        }

        private void RelaseRes(object sender, EventArgs e)
        {
            gvRes.EndEdit();
            int[] rhs = gvRes.GetSelectedRows();
            //bool stop = false;
            //foreach(int rh in rhs)
            //    if(gvRes.GetRowCellValue(rh, "ReservedBy").ToString()==string.Empty)
            //    {
            //        stop = true;
            //        break;
            //    }

            //if(stop)
            //{
            //    XtraMessageBox.Show("You can't release Karbel's reservations.");
            //    return;
            //}            

            if (rhs.Length > 0)
            {
                if (XtraMessageBox.Show("Are you sure to release selected reservation(s)?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    int espCount = 0;
                    List<int> ayrimIds = new List<int>();

                    foreach (int rh in rhs)
                    {
                        DataRow dr = gvRes.GetDataRow(rh);
                        espCount += dr["ESPRollCount"].ToIntZero();
                        ayrimIds.Add(dr["AyrimId"].ToIntNN());
                    }

                    if (espCount > 0)
                        XtraMessageBox.Show("Selected reservation(s) contains ESP articles. These ones will be released after Karbel's confirmation.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    ExecQuery("COReleaseRolls", 75, ConfExpert.Utility.AppSession.UserInfo.UserId, string.Join(",", ayrimIds));
                    gvRes.VeriyiGuncelle(true, "COGetReservations", 75);
                }
            }
            else
                XtraMessageBox.Show("Please select rows to release.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        }
    }
}
